﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using Microsoft.Windows.Controls;

using InfFront.InfoViews.Base;

namespace InfFront.InfoViews.WPF
{
    /// <summary>
    /// Interaction logic for SourceFrame.xaml
    /// </summary>
    public partial class SourceFrame : UserControl
    {
        #region Private Variables
        private InfoViewSource oldDataContext = null;
        #endregion

        #region Construction/Initialization
        public SourceFrame()
        {
            InitializeComponent();
        }

        public new InfoViewSource DataContext
        {
            get { return (InfoViewSource)base.DataContext; }
            set { base.DataContext = value; }
        }

        private AppGlobals AppGlobals
        {
            get
            {
                if (DataContext != null)
                    return DataContext.GetAppGlobals();
                return null;
            }
        }

        #endregion

        private void this_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (oldDataContext != null)
            {
                oldDataContext.OnRebuilt -= source_Rebuilt;
                oldDataContext.OnListRefreshed -= source_ListRefreshed;
                oldDataContext.OnEditEntry -= source_OnEditEntry;
                oldDataContext.OnCreateEntry -= source_OnCreateEntry;
            }

            oldDataContext = DataContext;
            if (oldDataContext != null)
            {
                oldDataContext.OnRebuilt += source_Rebuilt;
                oldDataContext.OnListRefreshed += source_ListRefreshed;
                oldDataContext.OnEditEntry += source_OnEditEntry;
                oldDataContext.OnCreateEntry += source_OnCreateEntry;
            }

            Rebuild();

            Visibility = (oldDataContext != null) ? Visibility.Visible : Visibility.Hidden;
        }

        public void Rebuild()
        {
            var curContent = content.Content as FrameworkElement;
            if (curContent != null)
                curContent.DataContext = null;
            content.Content = null;

            if (DataContext == null || DataContext.IsDummy)
            {
                return;
            }

            var raws = (RawString)DataContext.VisualSettings.GetValue(BaseSettingsManager.FORMATTED_PAGE_DESCRIPTION_KEY);
            descTextBlock.Visibility = string.IsNullOrEmpty(raws.Value) ? Visibility.Collapsed : Visibility.Visible;

            content.Content = AppGlobals.MainFrame.SelectContentFrameFor(DataContext);
        }

        public void RefreshList()
        {
            var frame = (BaseSourceContentFrame)content.Content;
            frame.Refresh();
        }

        private void PreferencesMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var globals = AppGlobals;
            if (globals == null)
                return;
            globals.MainFrame.ShowPreferences(DataContext);
        }

        void source_Rebuilt(object sender, EventArgs e)
        {
            Rebuild();
        }

        void source_ListRefreshed(object sender, EventArgs e)
        {
            RefreshList();
        }

        void source_OnCreateEntry(object sender, EntryEditEventArgs args)
        {
            var mainWindow = AppGlobals.MainFrame;
            using (mainWindow.SuppressProgress())
            {
                var wnd = new EntryEditWindow(args.Entry);
                wnd.Owner = Application.Current.MainWindow;
                wnd.ShowDialog();
            }
        }

        void source_OnEditEntry(object sender, EntryEditEventArgs args)
        {
            var mainWindow = AppGlobals.MainFrame;
            using (mainWindow.SuppressProgress())
            {
                var wnd = new EntryEditWindow(args.Entry);
                wnd.Owner = Application.Current.MainWindow;
                wnd.ShowDialog();
            }
        }

    }
}
